package com.xiashitech.apiservice.util;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.io.*;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import lombok.var;

import com.csvreader.*;
import com.google.common.collect.Lists;
import org.springframework.web.multipart.MultipartFile;

public class CsvIoUtil {
    private static final Log log = LogFactory.get();

    public static List<String[]> parseCsv(MultipartFile file) {
        List<String[]> result = Lists.newArrayList();
        try (var reader = file.getInputStream()) {
            var csvReader = new CsvReader(reader, StandardCharsets.UTF_8);
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                var items = csvReader.getValues();
                result.add(items);
            }
        } catch (IOException e) {
            log.info(e);
        }
        return result;
    }

    public static List<String[]> parseCsv(String filePath) throws IOException {
        List<String[]> result = Lists.newArrayList();
        var csvReader = new CsvReader(filePath, ',', StandardCharsets.UTF_8);
        csvReader.readHeaders();
        while (csvReader.readRecord()) {
            result.add(csvReader.getValues());
        }
        csvReader.close();
        return result;
    }
}
